<?php

namespace app\systems\logic;
use app\common\service\CommonExportCsvTaskLogService;
use think\facade\Db;

class SystemExportLogic extends BasicLogic
{


    /**
     * @desc    导出数据列表
     * @date    2024-08-13
     * @author  luckyXiong
     */
    public function get_list($params=[]){
        //实时查询
        $limit         = $params['limit'] ?? 10;
        $date_type     = $params['status'] ??'';
        $current_status= $params['current_status'] ??'';
        $createtime    = $params['createtime'] ?? "";

        $where = [];

        if ($createtime) {
            $where[] = ['created_unix', '>=', strtotime($createtime['0'])];
            $where[] = ['created_unix', '<=', strtotime($createtime['1']) + 3600*24-1];
        }

        if ($date_type) {
            $where[] = ['data_type', '=', $date_type];
        }

        if ($current_status) {
            $where[] = ['status', '=', $current_status];
        }

        if ($current_status=='0') {
            $where[] = ['status', '=', 0];
        }

        $field= 'id,task_name,data_type,path,rows_sum,done_sum,created_work_no,status,created_unix';

        $list = Db::name('common_export_csv_task_log')
            ->field($field)
            ->where($where)
            ->order('id desc')
            ->paginate($limit);

        $lists = $list->items();

        //批量获取导出者名字
        $created_work_nos = array_column($lists,'created_work_no');
        $workers          = Db::name('system_admin')->field('username,phone')->whereIn('phone',$created_work_nos)->select()->toArray();
        $workers_map      = array_column($workers,null,'phone');
        $info = (new CommonExportCsvTaskLogService())::EXPORT_TYPE_MAP;
        $status_info = Config('Logisticskey.export_status');
        $upload_config = sysconfig('upload');
        $domain = isset($upload_config['upload_url']) ? $upload_config['upload_url'] : '';
        foreach($lists as $k=>$v){
            $workers_map_info = $workers_map[$v['created_work_no']]??[];
            $lists[$k]['created_work_no'] = $workers_map_info['username']??'';
            $lists[$k]['data_type']       = $info[$v['data_type']]??'';
            $lists[$k]['status']          = $status_info[$v['status']]??'';
            $lists[$k]['path']            = $domain.'/'.$v['path'];
            $lists[$k]['created_unix']    = date("Y-m-d H:i:s", $v['created_unix']);

        }

        $data = [
            'list'  => $lists,
            'total' => $list->total(),
        ];
        return json(DefaultResponse(200, true, '获取成功', $data));
    }


    /**
     * @desc    获取导出数据类型
     * @date    2024-08-13
     * @author  luckyXiong
     */
    public function getDataType(){
        $info = (new CommonExportCsvTaskLogService())::EXPORT_TYPE_MAP;
        $info = getLevel($info);
        foreach($info as $v){
            $arr[] = [
                'data_type'   => $v['key'],
                'datatype_name' => $v['value'],
            ];
        }
        return json(DefaultResponse(200, true, '获取成功', $arr));

    }


    /**
     * @desc    删除数据
     * @date    2024-12-05
     * @author  luckyXiong
     */
    public function delete($params=[]){
       $id = $params['id']??'';
       if($id){
               Db::name('common_export_csv_task_log')->where(['id'=>$id])->delete();
           }
       return  $this->jsonMessage(200,true,'操作成功',null);
    }












}